package com.adv.crm.controller;

import com.adv.crm.beans.CustomerBean;
import com.adv.crm.beans.WarpPageResponse;
import com.adv.crm.beans.WarpResponse;
import com.adv.crm.services.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by 16978 on 2016/6/18.
 */
@Controller
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @RequestMapping("index")
    public String customerIndexView() {
        return "customer";
    }

    @RequestMapping("json/query")
    @ResponseBody
    public WarpPageResponse<List<CustomerBean>> queryCustomer(@RequestParam(defaultValue = "1") int pageIndex, @RequestParam(defaultValue = "20") int pageSize) {
        WarpPageResponse<List<CustomerBean>> response = new WarpPageResponse<List<CustomerBean>>();
        response = customerService.getCustomers(pageIndex, pageSize);
        return response;
    }

    @RequestMapping("json/all")
    @ResponseBody
    public WarpResponse<List<CustomerBean>> queryCustomer() {
        WarpResponse<List<CustomerBean>> response = customerService.getAllCustomer();
        return response;
    }

    @RequestMapping("json/add")
    @ResponseBody
    public WarpResponse<String> addCustomer(CustomerBean bean) {
        WarpResponse<String> response = customerService.insertCustomer(bean);
        return response;
    }

    @RequestMapping("json/delete/{id}")
    @ResponseBody
    public WarpResponse<String> deleteCustomerById(@PathVariable int id) {
        WarpResponse<String> response = customerService.deleteCustomerById(id);
        return response;
    }

    @RequestMapping("json/update")
    @ResponseBody
    public WarpResponse<String> updateCustomerById(CustomerBean bean) {
        WarpResponse<String> response = customerService.updateCustomer(bean);
        return response;
    }
}
